package com.loe.ktui

import android.app.Activity
import android.content.res.Resources
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.loe.ktui.ui.UiFrameLayout
import com.loe.ktui.ui.UiGroup
import com.loe.ktui.ui.UiText
import com.loe.ktui.ui.UiView
import com.loe.ktui.util.UiShape

const val WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT
const val MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT

const val HORIZONTAL = LinearLayout.HORIZONTAL
const val VERTICAL = LinearLayout.VERTICAL

object Direct
{
    const val LEFT = "left"
    const val TOP = "top"
    const val RIGHT = "right"
    const val BOTTOM = "BOTTOM"
}

inline val Any?.dist: Int
    get()
    {
        if (this is String)
        {
            return toInt()
        }
        if (this == WRAP_CONTENT || this == MATCH_PARENT)
        {
            return this as Int
        }
        if (this != null)
        {
            return (toString().toDouble() * Resources.getSystem().displayMetrics.density + 0.5).toInt()
        }
        return 0
    }

inline val Any?.distFloat: Float
    get()
    {
        if (this is String)
        {
            return toFloat()
        }
        if (this != null)
        {
            return toString().toFloat() * Resources.getSystem().displayMetrics.density
        }
        return 0f
    }

fun <T : View> ViewGroup.findView(id: String): T = findViewById(KtUi.getId(id))
fun <T : View> Activity.findView(id: String): T = findViewById(KtUi.getId(id))
fun <T : View> Fragment.findView(id: String): T = view!!.findViewById(KtUi.getId(id))

fun Activity.setContentUI(id: String? = null, block: (UiFrameLayout.() -> Unit)? = null)
{
    val ui = UiFrameLayout(id)
    block?.let { ui.it() }
    setContentView(ui.createView(this))
}

inline var Activity.contentUI: UiView?
    get() = null
    set(value)
    {
        setContentView(value?.createView(this))
    }

/******************************************* 工具 ******************************************/

internal inline fun safe(run: () -> Unit)
{
    try
    {
        run()
    } catch (e: Exception)
    {
        Log.e("KtUiRuntime", "" + e)
    }
}

internal fun TextView?.addTextAfterListener(after: (s: String) -> Unit)
{
    this?.addTextChangedListener(object : TextWatcher
    {
        override fun afterTextChanged(s: Editable?)
        {
            after(s.toString())
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int)
        {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int)
        {
        }
    })
}

internal fun View?.setDelayClickListener(delay: Long = 0, interval: Long = 1200, onClick: (v: View) -> Unit)
{
    var t = 0L
    this?.setOnClickListener()
    {
        if (System.currentTimeMillis() - t > interval)
        {
            postDelayed({ onClick(it) }, delay)
            t = System.currentTimeMillis()
        }
    }
}

val mainHandler by lazy { Handler(Looper.getMainLooper()) }

internal fun delay(delay: Long, run: () -> Unit)
{
    mainHandler.postDelayed(run, delay)
}

internal fun delay(run: () -> Unit)
{
    mainHandler.post(run)
}

internal fun px(dp: Double): Int
{
    return (dp * Resources.getSystem().displayMetrics.density + 0.5).toInt()
}
